import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter_magic_planet/common/http/request.dart';
import 'package:flutter_magic_planet/model/video.dart';
import 'package:flutter_magic_planet/model/video_chapter.dart';

class APIVideoBljiex {
  static const ID = 'vip-bljiex-com';

  /// https://vip.bljiex.com/api.php?out=json&wd=doll&cb=jQuery18209587534066243315_1629191210109&_=1629191210142
  static const URL_BLJIEX = 'https://vip.bljiex.com/api.php';

  static const _EXCLUDE_TYPE = [
    '精品推荐',
    '动漫精品',
    '成人动漫',
    '亚洲情色',
    '亚洲有码',
    '国产情色',
    '国产主播',
    '国产精品',
    '少女萝莉',
    '网红主播',
    '强奸乱伦',
    '中文字幕',
    '日韩精品',
    '欧美精品',
    '欧美性爱',
    '欧美情色',
    '人兽性交',
    '国产自拍',
    '口交视频',
    '3P合辑',
    '大秀视频',
    '无码专区',
    '日本无码',
    '日韩无码',
    '日本有码',
    '有码视频',
    '巨乳系列',
    '巨乳美乳',
    '教师学生',
    '制服诱惑',
    '邻家人妻',
    '熟女人妻',
    '人妻系列',
    'SM重味',
    '偷拍自拍',
    '自拍偷拍',
    '变态另类',
    '长腿丝袜',
    '丝袜长腿',
    '女同性恋',
  ];

  /// 视频搜索
  static Future<List<Video>> getVideos({required String key}) async {
    final Map response = await Request.get(
      action:
          '$URL_BLJIEX?out=json&wd=$key&cb=jQuery18209587534066243315_1629191210109&_=1629191210142',
      options: Options(
        headers: {
          'authority': 'vip.bljiex.com',
          'pragma': 'no-cache',
          'cache-control': 'no-cache',
          'sec-ch-ua':
              '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
          'accept':
              'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
          'dnt': 1,
          'x-requested-with': 'XMLHttpRequest',
          'sec-ch-ua-mobile': '?0',
          'user-agent':
              'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
          'sec-fetch-site': 'same-origin',
          'sec-fetch-mode': 'cors',
          'sec-fetch-dest': 'empty',
          'referer': 'https://vip.bljiex.com/so.php?wd=%E6%88%91%E5%9C%A8',
          'accept-language': 'zh-CN,zh;q=0.9',
          'cookie':
              'UM_distinctid=17b533df4d4af7-0f5bf9f17a432d-35637203-1ea000-17b533df4d5802; url_num=0; list_num=0; PHPSESSID=fo823e8kennddc45nkupti6akr; CNZZDATA1280214288=66672144-1629183799-%7C1629189202; live_num=; x2xct=SG',
        },
      ),
    );
    if (response['code'] == 0) {
      List data = response['info'];
      return data
          .where((f) => !_EXCLUDE_TYPE.contains(f['type']))
          .map((e) => Video(
                id: int.parse(e['id'] ?? '0'),
                videoId: e['id'],
                videoType: e['type'],
                cover: e['img'],
                title: Uri.decodeFull(e['title']),
                descs: e['from'],
              ))
          .toList();
    }
    return [];
  }

  static Future<List<VideoChapter>> getVideoChapters(
      {required String id}) async {
    final Map response = await Request.get(
      action:
          '$URL_BLJIEX?out=json&flag=0&id=$id&cb=jQuery18203452419647424272_1629191193958&_=1629191544846',
      options: Options(
        headers: {
          'authority': 'vip.bljiex.com',
          'pragma': 'no-cache',
          'cache-control': 'no-cache',
          'sec-ch-ua':
              '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
          'accept':
              'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
          'dnt': 1,
          'x-requested-with': 'XMLHttpRequest',
          'sec-ch-ua-mobile': '?0',
          'user-agent':
              'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
          'sec-fetch-site': 'same-origin',
          'sec-fetch-mode': 'cors',
          'sec-fetch-dest': 'empty',
          'referer': 'https://vip.bljiex.com/so.php?wd=%E6%88%91%E5%9C%A8',
          'accept-language': 'zh-CN,zh;q=0.9',
          'cookie':
              'UM_distinctid=17b533df4d4af7-0f5bf9f17a432d-35637203-1ea000-17b533df4d5802; url_num=0; list_num=0; PHPSESSID=fo823e8kennddc45nkupti6akr; CNZZDATA1280214288=66672144-1629183799-%7C1629189202; live_num=; x2xct=SG',
        },
      ),
    );
    LogUtil.v(response);
    if (response['code'] == 200) {
      List<dynamic> data = response['info'][0]['video'];
      return data.map((e) {
        List<String> arr = e.split('\$');
        return VideoChapter(
          title: arr[0],
          chapterUrl: arr[1],
        );
      }).toList();
    }
    return [];
  }
}
